package SeparateAnalysis;

import java.text.DecimalFormat;

/**
 * Created by IntelliJ IDEA.
 * User: Mike
 * Date: 11/19/11
 * Time: 12:24 PM
 * To change this template use File | Settings | File Templates.
 */
public class VaporAbv {

    private String columnTemp;
    private String vaporAbv;
    private double[] convTemp = {100, 98.8888888888889, 98.5, 97.3888888888889, 96.5, 95.5, 94.5, 93.5, 92.5, 92, 91, 90.5, 88, 86, 83.5, 81.2222222222222, 80.6111111111111, 80.2222222222222, 79.7777777777778, 79, 78.7777777777778, 78.3888888888889, 78.2222222222222, 78.1111111111111};
    private double[] convAbv = {0, 12, 18, 28, 36, 43, 50, 54, 59, 61, 65, 67, 75, 79, 83, 86, 87, 88, 89, 90, 91, 92, 95, 96.5};


    public VaporAbv(String columnTemp) {
        this.columnTemp = columnTemp;
        double colTempD = Double.valueOf(columnTemp);
        if (colTempD > 78.1 && colTempD <= 100) {
            vaporAbv = String.valueOf(getVaporAbv(columnTemp));
            System.out.println(vaporAbv);
        } else {
            vaporAbv = "0";
        }
    }

    public String getVaporAbv() {
        return vaporAbv;
    }

    private double getVaporAbv(String temp) {
        Integer index = 0;
        double tempD = Double.valueOf(temp);
        while (tempD < convTemp[index]) {                            // Binary Search the array (since non exact values are used)
            index++;
        }
        double numeratorDiff = convAbv[index - 1] - convAbv[index];        // linear interpolation
        double denomDiff = convTemp[index - 1] - convTemp[index];
        double abv = numeratorDiff / denomDiff;
        abv = (tempD - convTemp[index]) * abv + convAbv[index];
        DecimalFormat df1 = new DecimalFormat("##.#");
        abv = Double.parseDouble(df1.format(abv));
        return abv;
    }
}
